<template>
  <div class="container">
    <div class="login-box">
      <el-form :model="form" label-width="80px" :rules="rules" ref="form">
        <el-form-item label="账号:" prop="userName">
          <el-input v-model="form.userName" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="form.password" type="password" placeholder=""></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-button type="primary" @click="login">登录</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { query } from '@/tools/mysql';
export default {
  name: 'Login',
  data() {
    return {
      form: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          { required: true, min: 1, max: 11, message: '请输入1-11位账号', trigger: 'blur' },
        ],
        password: [{ required: true, min: 1, max: 18, trigger: 'blur', message: '请输入密码' }]
      }
    };
  },
  created() {

  },
  mounted() {

  },
  methods: {
    login() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = await query(`SELECT password,user_name FROM user WHERE user_name = ${this.form.userName}`);
          console.log(res);
          if (!res.length) {
            this.$message.error('用户不存在')
          } else {
            res = res[0] || {};
            if (res.password == this.form.password) {
              this.$message.success('登录成功');
              setTimeout(() => {
                this.$router.push('/home')
              }, 700);
            } else {
              this.$message.error('密码错误')
            }
          }
        } else {
          return false
        }
      })
    }
  },
};
</script>

<style scoped lang="scss">
.container {
  width: 100%;
  height: 100%;
  background-image: linear-gradient(135deg, #1188ff, transparent);
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box {
    width: 300px;
    // height: 500px;
    // border: 1px solid #eee;
  }
}
</style>
